package petstore.persistence;

import petstore.domain.CartItem;
import petstore.domain.Item;
import petstore.persistence.impl.CartDao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao
{
    private final int uid;
    private static final String GET_CART_ITEMS = "SELECT user_id,itemId,quantity,price FROM cart WHERE user_id = ";
    List<CartItem> cartItemList;

    public CartDaoImpl(int uid)
    {
        this.uid = uid;
    }

    @Override
    public List<CartItem> getCartItemList()
    {
        if(cartItemList == null)
        {
            cartItemList = new ArrayList<>();
        }
        try
        {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(GET_CART_ITEMS+uid);
            while(resultSet.next())
            {
                CartItem cartItem = new CartItem();
                cartItem.setUid(resultSet.getInt("user_id"));
                cartItem.setQuantity((resultSet.getInt("quantity")));
                cartItem.setItem(new ItemDaoImpl().getItem(resultSet.getString("itemId")));
                cartItem.setPrice(resultSet.getBigDecimal("price"));
                cartItemList.add(cartItem);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("cartItemList:");
        for(CartItem cartItem:cartItemList)
        {
            System.out.println(cartItem.getItem().getName()+" price:"+cartItem.getPrice());
        }
        return cartItemList;
    }

    @Override
    public int addItem(String itemId,int quantity)
    {
        if(cartItemList == null)
        {
            getCartItemList();
        }
        for (CartItem cartItem : cartItemList)
        {
            if (cartItem.getItem().getItemId().equals(itemId))//购物车中已有该item
            {
                try
                {
                    int tempQuantity = cartItem.getQuantity() + quantity;
                    cartItem.setQuantity(tempQuantity);
                    Connection connection = DBUtil.getConnection();
                    Statement statement = connection.createStatement();
                    statement.executeUpdate("UPDATE cart SET quantity = " + tempQuantity + " WHERE user_id = '" + uid + "' AND itemId = '" + itemId + "'");
                    connection.close();
                    statement.close();
                    return tempQuantity;
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

        //该item未在购物车中
        try
        {
            Item tempItem = new ItemDaoImpl().getItem(itemId);
            cartItemList.add(new CartItem(uid,tempItem,quantity,tempItem.getListPrice()));
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO cart VALUES ('"+ uid +"','"+itemId+"','" + quantity + "','" + tempItem.getUnitPrice() + "')");
            connection.close();
            statement.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return quantity;
    }

    @Override
    public int subItem(String itemID)
    {
        if(cartItemList == null)
        {
            getCartItemList();
        }
        for (CartItem cartItem : cartItemList)
        {
            if(itemID.equals(cartItem.getItem().getItemId()))
            {
                try
                {
                    int tempQuantity = cartItem.getQuantity()-1;
                    cartItem.setQuantity(tempQuantity);
                    Connection connection = DBUtil.getConnection();
                    Statement statement = connection.createStatement();
                    if(tempQuantity == 0)
                        statement.executeUpdate("DELETE FROM cart WHERE user_id = '" + uid + "'and itemId = '" + itemID + "'");
                    else
                        statement.executeUpdate("UPDATE cart SET quantity = '" + (tempQuantity) + "' WHERE user_id = '" + uid + "' AND  itemId = '"+ itemID + "'");
                    connection.close();
                    statement.close();
                    return tempQuantity;
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        return -1;
    }

    @Override
    public int removeItem(String itemID)
    {
        if(cartItemList == null)
        {
            getCartItemList();
        }
        for (int i = 0;i < cartItemList.size();i++)
        {
            if(itemID.equals(cartItemList.get(i).getItem().getItemId()))
            {
                try
                {
                    cartItemList.remove(i);
                    Connection connection = DBUtil.getConnection();
                    Statement statement = connection.createStatement();
                    statement.executeUpdate("DELETE FROM cart WHERE user_id = '" + uid + "'and itemId = '" + itemID + "'");
                    connection.close();
                    statement.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    public static void main(String[] args)
    {
        CartDaoImpl test = new CartDaoImpl(1);
        System.out.println(test.getCartItemList());
    }
}
